home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Magazine / C_Tutorial / Part-9 / wb1 / arexx.c next >
C/C++ Source or Header  |  1997-11-26  |  2KB  |  75 lines

  1. #include "arexx.h"
  2.  
  3. #include<clib/alib_protos.h>
  4. #include<clib/exec_protos.h>
  5. #include<clib/rexxsyslib_protos.h>
  6.  
  7. #include<stdio.h>
  8. #include<string.h>
  9.  
  10. static struct MsgPort* arexxport = NULL;
  11.  
  12. int createARexxPort(char* portname)
  13. {
  14.   int success = FALSE;
  15.     char* error = NULL;
  16.     /* First, must turn off multi-tasking so we can atomicly add our port */
  17.     Forbid();
  18.   /* We can only succeed if the named port doesn't already exist */
  19.     if(FindPort(portname) == NULL)
  20.     {
  21.         /* At last, make the port (priority 1 to enable fast searching) */
  22.         if(arexxport = CreatePort(portname, 1))
  23.             success = TRUE;
  24.         else
  25.             error = "Error: cannot create ARexx port\n";
  26.     }
  27.     else
  28.         error = "Error: cannot create ARexx port (the name already exists)\n";
  29.     /* We must now turn multi-tasking back on... */
  30.     Permit();
  31.   /* ...now we can print any error and return the result */
  32.   if(error)
  33.     printf(error);
  34.     return success;
  35. }
  36.  
  37. void freeARexxPort()
  38. {
  39.     if(arexxport)
  40.     {
  41.         struct RexxMsg* msg;
  42.         /* First, make the port private so we get no more messages */
  43.         RemPort(arexxport);
  44.         /* Now clear out any outstanding messages... */
  45.         /* (Passing 20 for rc indicates an error reply) */
  46.         while(msg = getARexxMsg())
  47.             replyARexxMsg(msg, 20, NULL);
  48.         /* ...and now it's safe to delete the port */
  49.         DeletePort(arexxport);
  50.         arexxport = NULL;
  51.     }
  52. }
  53.  
  54. ULONG getARexxSig()
  55. {
  56.     return arexxport ? 1L << arexxport->mp_SigBit : 0L;
  57. }
  58.  
  59. struct RexxMsg* getARexxMsg()
  60. {
  61.     return (struct RexxMsg*)GetMsg(arexxport);
  62. }
  63.  
  64. void replyARexxMsg(struct RexxMsg* msg, LONG rc, char* result)
  65. {
  66.     /* Set up reply result values */
  67.     msg->rm_Result1 = rc;
  68.     msg->rm_Result2 = NULL;
  69.     /* If the reply expects a result string, create one */
  70.     if((msg->rm_Action & RXFF_RESULT) && rc == 0 && result != NULL)
  71.         msg->rm_Result2 = (LONG)CreateArgstring(result, strlen(result));
  72.     /* Now we can reply to the message */
  73.     ReplyMsg((struct Message*)msg);
  74. }
  75.